package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.MediaConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.MediaFile;
import cn.itsource.ymcc.service.IMediaFileService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
@RocketMQMessageListener(
        consumerGroup = "MediaConsumer",
        topic = MediaConstants.TOPIC_COURSE_VIDEO_PUSH_TO_SRS,
        selectorExpression = MediaConstants.TAGS_COURSE_VIDEO_PUSH_TO_SRS
)
public class MediaConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService iMediaFileService;
    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            byte[] body = messageExt.getBody();
            String message = new String(body, Constants.UTF8);
            MediaFile mediaFile = JSONObject.parseObject(message, MediaFile.class);
            iMediaFileService.handleFile2m3u8(mediaFile);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
